package com.acoffee.controller;

import com.acoffee.pojo.RbacPerm;
import com.acoffee.pojo.ResponseEntity;
import com.acoffee.service.RbacPermService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author acoffee
 * @create 2021-11-27 14:11
 */
@RestController
public class RbacPermController {
    @Autowired
    RbacPermService rbacPermService;

    @GetMapping("/queryAllPerm/{startPage}/{pageSize}")
    public ResponseEntity<PageInfo<RbacPerm>> queryAll(@PathVariable int startPage,@PathVariable int pageSize){
        PageHelper.startPage(startPage,pageSize);
        List<RbacPerm> rbacPerms = rbacPermService.queryAll();
        PageInfo<RbacPerm> pageInfo = new PageInfo<>(rbacPerms);
        ResponseEntity<PageInfo<RbacPerm>> entity = new ResponseEntity<>();
        entity.setCode(200);
        entity.setMsg("查询成功");
        entity.setData(pageInfo);
        return entity;
    }

    @GetMapping("/queryPermById/{id}")
    public ResponseEntity<RbacPerm> queryOne(@PathVariable int id){
        RbacPerm perm = rbacPermService.queryOne(id);
        ResponseEntity<RbacPerm> entity = new ResponseEntity<>();
        entity.setCode(200);
        entity.setMsg("查询成功");
        entity.setData(perm);
        return entity;
    }

    @PostMapping("/updatePermById")
    public ResponseEntity updateOne(RbacPerm perm){
        boolean flag = rbacPermService.updateOne(perm);
        ResponseEntity<Object> entity = new ResponseEntity<>();
        if(flag){
            entity.setCode(200);
            entity.setMsg("修改成功！");
            entity.setData(perm);
        }else{
            entity.setCode(303);
            entity.setMsg("修改失败！");
        }
        return entity;
    }

    @PutMapping("/addPerm")
    public ResponseEntity addOne(RbacPerm perm){
        boolean flag = rbacPermService.addOne(perm);
        ResponseEntity<Object> entity = new ResponseEntity<>();
        if(flag){
            entity.setCode(200);
            entity.setMsg("添加成功！");
            entity.setData(perm);
        }else{
            entity.setCode(303);
            entity.setMsg("添加失败！");
        }
        return entity;
    }

    @DeleteMapping("/deletePerm/{id}")
    public ResponseEntity deleteOne(@PathVariable  int id){
        boolean flag = rbacPermService.deleteOne(id);
        ResponseEntity<Object> entity = new ResponseEntity<>();
        if(flag){
            entity.setCode(200);
            entity.setMsg("删除成功！");
        }else{
            entity.setCode(303);
            entity.setMsg("删除失败！");
        }
        return entity;
    }
}
